<?php
#+------------------------------------------------------------------
#| 普通的。
#+------------------------------------------------------------------
#| Author:Janmas Cromwell <janmas-cromwell@outlook.com>
#+------------------------------------------------------------------
namespace form\element\form;

use form\element\form\traits\Options;
use form\element\form\traits\Value;
use form\element\traits\{
    Classes,
    Css,
    Id,
    Js
};

class Editor extends Column
{
    use Classes, Id, Css, Js, Value, Options;

    protected function getOptions(): string
    {
        if (empty($this->options)) {
            $this->options = [
                'width'          => "100%",
                'height'         => "600",
                'path'           => "http://libs.itshubao.com/editor.md/lib/",
                'codeFold'       => true,
                'htmlDecode'     => 'iframe|on*', // 开启标签
                'imageUpload '   => true,
                'imageUploadURL' => "http://example.itshubao.com/index/test/upload/type/editormd",
            ];
        }

        return is_array($this->options) ? json_encode($this->options, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE) : $this->options;
    }

    public function generate()
    {
        $label = $this->getLabel();
        $labelId = $this->getLabelFor();
        $labelClass = $this->getLabelClass();
        $name = $this->name;
        $class = $this->getClass();
        $id = $this->getId();
        $value = $this->value;
        $options = $this->getOptions();
        $this->css = <<<STYLE

/*模板样式里面有一个css会影响editor.md的图标,这里重设下*/
.editormd .divider {
    width: auto;
}
.editormd .divider:before,
.editormd .divider:after {
    margin: 0px;
}

STYLE;

        $this->loadJs = [
            'http://libs.itshubao.com//editor.md/editormd.min.js',
        ];

        $this->loadCss = [
            'http://libs.itshubao.com//editor.md/css/editormd.min.css'
        ];
        $this->js = <<<HTML

$(function() {
    var editor = editormd("$labelId", {$options});
});

HTML;

        return <<<HTML

<div class="$class">
<label for="$labelId" class="$labelClass">$label</label>
<div id="$id">
<textarea name="$name" style="display: none">$value</textarea>
</div>
</div>
HTML;

    }

    /**
     * @inheritDoc
     */
    protected function normal()
    {
        // TODO: Implement normal() method.
    }

    /**
     * @inheritDoc
     */
    protected function vertical()
    {
        // TODO: Implement vertical() method.
    }

    /**
     * @inheritDoc
     */
    protected function inline()
    {
        // TODO: Implement inline() method.
    }
}